﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JobBack.Controllers
{
    public class FileUploadController : Controller
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase file)
        {

            //先定义文件
            string fileName = Guid.NewGuid().ToString();
            //图片存放的路径
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Pic");
            //如果没有图片
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            //获取图片的扩展名
            string stuf = Path.GetExtension(file.FileName);

            //图片存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //保存图片
            string path = Path.Combine(filePath, fileName + stuf);
            file.SaveAs(path);
            string ulr = "/Pic/" + fileName + stuf;
            //返回前端
            return Json(new { Success = true, Url = ulr });          

        }
    }
}